<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.codeKeyword" placeholder="字典代码关键词" style="width: 130px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.nameKeyword" placeholder="字典名称关键词" style="width: 130px; margin-left: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-circle-plus-outline" @click="handleAdd">
        添加
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" border stripe element-loading-text="拼命加载中">
      <el-table-column label="字典代码" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span>{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="字典名称" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类数量" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.details | detailsFilte }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/system/data-dictionary-detail/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">
              详情
            </el-button>
          </router-link>
          <el-button type="danger" size="mini" style="margin-left: 5px;" icon="el-icon-delete" @click="handleDelete(scope)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog title="字典添加" :visible.sync="dataDictAddVisible">
      <el-form ref="dataDictAddForm" :rules="dataDictAddRules" :model="dataDictAdd" label-width="80px" style="width: 600px; margin-left:0px;">
        <el-form-item label="字典代码" prop="code">
          <el-input v-model="dataDictAdd.code" />
        </el-form-item>
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="dataDictAdd.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dataDictAddVisible = false">取消</el-button>
        <el-button type="primary" @click="addConfirm()">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getDataDictByPage, addDataDict, deleteDataDictById } from '@/api/system-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'SystemDataDictionaryList',
  components: { Pagination },
  directives: { waves },
  filters: {
    detailsFilte(details) {
      if (details) {
        return details.length
      }
      return 0
    }
  },
  data() {
    return {
      beforeRouteEnterNextIgnore: true,

      listQuery: {
        pageNo: 1,
        pageSize: 20,
        codeKeyword: null,
        nameKeyword: null
      },

      listLoading: true,
      list: null,
      total: 0,

      dataDictAddVisible: false,
      dataDictAdd: {
        code: null,
        name: null
      },
      dataDictAddRules: {
        code: [{
          required: true, message: '请输入字典代码', trigger: 'blur'
        }],
        name: [{
          required: true, message: '请输入字典名称', trigger: 'blur'
        }]
      }

    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (!vm.beforeRouteEnterNextIgnore) {
        vm.getList()
      } else {
        vm.beforeRouteEnterNextIgnore = false
      }
    })
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getDataDictByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      })
    },

    handleFilter() {
      this.getList()
    },
    handleAdd() {
      this.dataDictAdd = {
        code: null,
        name: null
      }
      this.dataDictAddVisible = true
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },

    addConfirm() {
      this.$refs.dataDictAddForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.dataDictAdd))
        addDataDict(data).then(response => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
          this.dataDictAddVisible = false
          this.getList()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleDelete(dictScope) {
      deleteDataDictById(dictScope.row.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
